﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Media.Imaging;
using Windows.UI.Xaml.Navigation;

// Pour en savoir plus sur le modèle d'élément Contrôle utilisateur, consultez la page http://go.microsoft.com/fwlink/?LinkId=234236

namespace BlipSansBlop
{
    public enum EtatEnnemi { Marche = 1, Tombe = 2, Mort = 3 };
    public sealed partial class Ennemi : UserControl
    {
        public Point Position { get; set; }
        public Double DeplacementX { get; set; }
        public UIElement Support { get; set; }
        public Boolean HasSupport { get; set; }
        public int Vies { get; set; }
        public int ViesInit { get; set; }
        public Ennemi()
        {
            this.InitializeComponent();
            this.Position = new Point(Canvas.GetLeft(this), Canvas.GetTop(this));
            this.DeplacementX = 6;
            this.Vies = MainPage.rand.Next(8, 14);
            this.ViesInit = this.Vies;
            int randoom = MainPage.rand.Next(0,4);
            switch (randoom){
                case 0:
                    this.EnnemiComposite.TranslateY = 0;
                    break;
                case 1:
                    this.EnnemiComposite.TranslateY = (-55);
                    break;
                case 2:
                    this.EnnemiComposite.TranslateY = (-110);
                    break;
                case 3:
                    this.EnnemiComposite.TranslateY = (-165);
                    break;
            }
        }

        public void Update()
        {
            if (HasSupport)
            {
                if (this.GoesToSupport(DeplacementX))
                {
                    this.Position = new Point(Canvas.GetLeft(this) + DeplacementX, Canvas.GetTop(this));
                }
                else if (this.GoesToSupport(-DeplacementX))
                {
                    DeplacementX = (-DeplacementX);
                    this.Position = new Point(Canvas.GetLeft(this) + DeplacementX, Canvas.GetTop(this));
                }
                else HasSupport = false;

                if (this.HitTest(0, 0, MainPage.Obstacles))
                {
                    this.Position = new Point(Canvas.GetLeft(this) - DeplacementX, Canvas.GetTop(this));
                    DeplacementX = (-DeplacementX);
                }

                if (DeplacementX > 0) this.EnnemiUserControlCompo.ScaleX = (1);
                else this.EnnemiUserControlCompo.ScaleX = (-1);

            }
            else
            {
                this.Position = new Point(Canvas.GetLeft(this), Canvas.GetTop(this) + MainPage.Gravite);
                if (DetectSupport())
                {
                    this.Support = GetSupport();
                    this.HasSupport = true;
                }
            }
            if (HitTestVertical(-22, 0, MainPage.Tirs)) this.Hit();
            if (HitTestVertical(22, 0, MainPage.Tirs)) this.Hit();
            this.Draw();
        }

        public void Hit()
        {
            this.Vies--;
            if (this.Vies < 0) this.Kill();
            else this.ViesBar.Width = ((double)this.Vies / (double)this.ViesInit) * 60;
        }

        public void Kill()
        {
            MainPage.Ennemis.Children.Remove(this);
            if (MainPage.rand.NextDouble() > 0.8)
            {
                if (MainPage.rand.NextDouble() > 0.5) new Munitions(this.Position.X + 5, this.Position.Y + 25);
                else new CoeurVie(this.Position.X + 5, this.Position.Y + 25);
            }
        }

        public void SwitchImage()
        {
            if (this.EnnemiComposite.TranslateX == (-80)) this.EnnemiComposite.TranslateX = (-40);
            else this.EnnemiComposite.TranslateX = (-80);
        }

        public Boolean DetectSupport()
        {
            Point test = new Point(this.Position.X + this.Width / 2, this.Position.Y + this.Height + 1);
            if (VisualTreeHelper.FindElementsInHostCoordinates(test, MainPage.Obstacles).Count() != 0) return true;
            else return false;
        }

        public UIElement GetSupport()
        {
            Point test = new Point(this.Position.X + this.Width / 2, this.Position.Y + this.Height + 1);
            return VisualTreeHelper.FindElementsInHostCoordinates(test, MainPage.Obstacles).FirstOrDefault();
        }

        public Boolean GoesToSupport(double X)
        {
            Point test = new Point(this.Position.X + this.Width / 2 + X, this.Position.Y + this.Height + 1);
            if (VisualTreeHelper.FindElementsInHostCoordinates(test, MainPage.Obstacles).Contains(this.Support)) return true;
            return false;
        }

        public Boolean HitTest(double DecalageX, double DecalageY, UIElement DossierParent)
        {
            Point test = new Point(this.Position.X + DecalageX, this.Position.Y + (this.Height / 2) + DecalageY);

            for (int x = Convert.ToInt32(this.Position.X + DecalageX) + 4; x < Convert.ToInt32(this.Position.X + DecalageX + this.Width) - 4; x += 2)
            {
                test.X = x;
                IEnumerable<UIElement> List = VisualTreeHelper.FindElementsInHostCoordinates(test, DossierParent);
                if (List.Count() != 0) return true;
            }
            return false;
        }

        public Boolean HitTestVertical(double DecalageX, double DecalageY, UIElement DossierParent)
        {
            Point test = new Point(this.Position.X + (this.Width / 2) + DecalageX, this.Position.Y + DecalageY);

            for (int y = Convert.ToInt32(this.Position.Y + DecalageY) + 4; y < Convert.ToInt32(this.Position.Y + DecalageY + this.Height) - 4; y += 10)
            {
                test.Y = y;
                if (VisualTreeHelper.FindElementsInHostCoordinates(test, DossierParent).Count() != 0) return true;

            }
            return false;
        }

        private void Draw()
        {
            Canvas.SetLeft(this, Position.X);
            Canvas.SetTop(this, Position.Y);
        }
    }
}
